/* Select Device (Select one, comment out the others) */
#define _ATMEGA16
//#define _ATMEGA32
// #define _ATMEGA161 
//#define _ATMEGA163 

/* Select Boot Size (select one, comment out the others).
   NB!!! Remember to manually change the linker command file accordingly !!!
   i.e Set "Code = xxxx-xxxx" accordingly. See issue 1) under the Special 
   considerations section in the Appnote.*/
//#define _B128 
//#define _B256 
#define _B512 
//#define _B1024 
//#define _B2048

// Define pin for enter selfprogramming mode
#define PROGPORT PORTD
#define PROGPIN PIND
#define PROGCTRL (1<<PD2)
#define PROGMODE !(PROGPIN & PROGCTRL)  
// TRUE if PROGCTRL on PROGPIN is low

#ifdef _ATMEGA16 
  #include "iom16v.h"
  #define PARTCODE 0x75
  
  #define sig_byte1 0x1E
  #define sig_byte2 0x94
  #define sig_byte3 0x03
  #define PAGESIZE 128      //Size in Bytes
  
  #ifdef _B128
    #define APP_PAGES ((2*8192 / PAGESIZE)- (2*128 / PAGESIZE )) 
    #define APP_END APP_PAGES * PAGESIZE 
  #endif
  #ifdef _B256
    #define APP_PAGES ((2*8192 / PAGESIZE)- (2*256 / PAGESIZE )) 
    #define APP_END APP_PAGES * PAGESIZE 
  #endif
  #ifdef _B512
    #define APP_PAGES ((2*8192 / PAGESIZE)- (2*512 / PAGESIZE )) 
    #define APP_END APP_PAGES * PAGESIZE 
  #endif
   #ifdef _B1024
    #define APP_PAGES ((2*8192 / PAGESIZE)- (2*1024 / PAGESIZE )) 
    #define APP_END APP_PAGES * PAGESIZE 
  #endif
  #ifdef _B2048
    #error This part does not support 2048 word boot block
  #endif
#endif

#ifdef _ATMEGA32 
  #include "iom32v.h"
  #define PARTCODE 0x73
  
  #define sig_byte1 0x1E
  #define sig_byte2 0x95
  #define sig_byte3 0x01
  #define PAGESIZE 128      //Size in Bytes
  
  #ifdef _B128
    #error This part does not support 128 word boot block
  #endif
  #ifdef _B256
    #define APP_PAGES ((2*16384 / PAGESIZE)- (2*256 / PAGESIZE )) 
    #define APP_END APP_PAGES * PAGESIZE 
  #endif
  #ifdef _B512
    #define APP_PAGES ((2*16384 / PAGESIZE)- (2*512 / PAGESIZE )) 
    #define APP_END APP_PAGES * PAGESIZE 
  #endif
   #ifdef _B1024
    #define APP_PAGES ((2*16384 / PAGESIZE)- (2*1024 / PAGESIZE )) 
    #define APP_END APP_PAGES * PAGESIZE 
  #endif
  #ifdef _B2048
    #define APP_PAGES ((2*16384 / PAGESIZE)- (2*2048 / PAGESIZE )) 
    #define APP_END APP_PAGES * PAGESIZE 
  #endif
#endif

#ifdef _ATMEGA161
  #include "iom161v.h"
  #define PARTCODE 0x61

  #define sig_byte1 0x1E
  #define sig_byte2 0x94
  #define sig_byte3 0x01
  #define PAGESIZE 128       //Size in Bytes
  #ifdef _B128
    #error The ATmega161 only supports 512 words boot block
  #endif
  #ifdef _B256 
    #error The ATmega161 only supports 512 words boot block
  #endif
  #ifdef _B512 
    #define APP_PAGES ((2*8192 / PAGESIZE )- (2*512 / PAGESIZE )) 
    #define APP_END APP_PAGES * PAGESIZE 
  #endif
  #ifdef _B1024 
    #error The ATmega161 only supports 512 words boot block
  #endif
  #ifdef _B2048 
    #error The ATmega161 only supports 512 words boot block
  #endif
#endif

#ifdef _ATMEGA163
  #include "iom163v.h"
  #define PARTCODE 0x66

  #define sig_byte1 0x1E
  #define sig_byte2 0x94
  #define sig_byte3 0x02
  #define PAGESIZE 128      //Size in Bytes
  
  #ifdef _B128
    #error This program does not fit in 128 words, minimum 256 words
  #endif
  #ifdef _B256
    #define APP_PAGES ((2*8192 / PAGESIZE)- (2*256 / PAGESIZE )) 
    #define APP_END APP_PAGES * PAGESIZE 
  #endif
  #ifdef _B512
    #define APP_PAGES ((2*8192 / PAGESIZE)- (2*512 / PAGESIZE )) 
    #define APP_END APP_PAGES * PAGESIZE 
  #endif
   #ifdef _B1024
    #define APP_PAGES ((2*8192 / PAGESIZE)- (2*1024 / PAGESIZE )) 
    #define APP_END APP_PAGES * PAGESIZE 
  #endif  
  #ifdef _B2048
    #error This part does not support 2048 word boot block
  #endif
#endif

#ifdef _ATMEGA8515
  #define PARTCODE 0x3A
#endif

#ifdef _ATMEGA128
  #define PARTCODE 0x44
#endif

